using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Threading.Tasks;
using Tea;

namespace openapi_sdk_v3
{
    [Icon("pack://application:,,,/openapi_sdk_v3;component/Resources/Icon.png")]
    [Designer("openapi_sdk_v3.Designer.openapi_sdk_v3ServerCommandDesigner, openapi_sdk_v3")]
    [Description("阿里云openapi市场SDK-V3版通用方法插件")]
    public class openapi_sdk_v3ServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        [Required]
        [Description("阿里云RAM账户中心的accessKeyId")]
        public object accessKeyId { get; set; }

        [FormulaProperty]
        [Required]
        [Description("阿里云RAM账户中心的accessKeySecret")]
        public object accessKeySecret { get; set; }

        [FormulaProperty]
        [Required]
        [Description("接口名称--Action")]
        [DisplayName("接口名称")]
        public object 接口名称 { get; set; }

        [FormulaProperty]
        [Required]
        [Description("接口版本--Version")]
        [DisplayName("接口版本")]
        public object 接口版本 { get; set; }

        [Required]
        [Description("接口协议--Protocol。")]
        [ComboProperty(ValueList = "HTTPS|HTTP", DisplayList = "HTTPS|HTTP")]
        public string 接口协议 { get; set; } = "HTTPS";

        [Required]
        [Description("接口HTTP方法--Method。")]
        [ComboProperty(ValueList = "POST|GET", DisplayList = "POST|GET")]
        public string 接口HTTP方法 { get; set; } = "POST";

        [FormulaProperty]
        [Required]
        [Description("接口HTTP方法--AuthType。")]
        public object 接口初始化方法 { get; set; } = "AK";

        [Required]
        [Description("接口HTTP方法--Style。")]
        [ComboProperty(ValueList = "ROA|RPC", DisplayList = "ROA|RPC")]
        public string 接口风格方法 { get; set; } = "RPC";

        [FormulaProperty]
        [Required]
        [Description("接口 PATH--Pathname")]
        [DisplayName("接口PATH")]
        public object 接口PATH { get; set; }


        [Required]
        [Description("接口响应格式。")]
        [ComboProperty(ValueList = "json|xml", DisplayList = "json|xml")]
        public string 接口响应格式 { get; set; } = "json";

        [ListProperty]
        public List<MyObj> 接口参数 { get; set; }

        //接口参数
        public class MyObj : ObjectPropertyBase
        {
            public string 参数名 { get; set; }

            [FormulaProperty]
            public object 参数值 { get; set; }
        }


        [ResultToProperty]
        [DisplayName("返回结果")]
        public string ResultTo { get; set; } = "返回结果";

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var accKeyId = await dataContext.EvaluateFormulaAsync(accessKeyId);
            var accKeySecret = await dataContext.EvaluateFormulaAsync(accessKeySecret);

            var add1 = await dataContext.EvaluateFormulaAsync(接口名称); // 计算的一个加数的公式值
            var add2 = await dataContext.EvaluateFormulaAsync(接口版本); // 计算的一个加数的公式值
            var add3 = await dataContext.EvaluateFormulaAsync(接口PATH); // 计算的一个加数的公式值
            var add4 = await dataContext.EvaluateFormulaAsync(接口初始化方法); // 计算的一个加数的公式值

            string ass1 = add1?.ToString() ?? string.Empty;//转换为字符串并判断是否空值
            string ass2 = add2?.ToString() ?? string.Empty;//转换为字符串并判断是否空值
            string ass3 = add3?.ToString() ?? string.Empty;//转换为字符串并判断是否空值
            string ass4 = add4?.ToString() ?? string.Empty;//转换为字符串并判断是否空值


            string acKeyId = accKeyId?.ToString() ?? string.Empty;//转换为字符串并判断是否空值
            string acKeySecret = accKeySecret?.ToString() ?? string.Empty;//转换为字符串并判断是否空值

            AlibabaCloud.OpenApiClient.Client client = aliyun.CreateClient(acKeyId, acKeySecret);
            AlibabaCloud.OpenApiClient.Models.Params params_ = aliyun.CreateApiInfo(ass1, ass2, 接口协议, 接口HTTP方法, ass4, 接口风格方法, ass3, "formData", 接口响应格式);

            //取时间
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();

            //获取参数
            //取接口参数列表
            List<MyObj> 参数列表 = 接口参数;
            Dictionary<string, object> body = new Dictionary<string, object>();
            foreach (MyObj 参数对象 in 参数列表)
            {
                string 参数名 = 参数对象.参数名.ToString();
                string 计算结果 = (await dataContext.EvaluateFormulaAsync(参数对象.参数值)).ToString();//参数值可能是一个公式                    
                body.Add(参数名, 计算结果); //ass1.Add(参数名, 计算结果);
            }

            //传入body参数
            AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest
            {
                Body = body,
            };
            Dictionary<string, string> headers = new Dictionary<string, string>() { };
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                var response = client.CallApi(params_, request, runtime);
                dataContext.Parameters[ResultTo] = response;
            }
            catch (TeaException error)
            {
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                dataContext.Parameters[ResultTo] = error.Message;
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 错误 message
                Console.WriteLine(error.Message);

                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);

                dataContext.Parameters[ResultTo] = error.Message;
            }

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "阿里云openapi通用SDK集成-V3";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}
